package com.example.testproject.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.testproject.R
import com.example.testproject.adapter.RvAdvancedParentAdapter
import com.example.testproject.bean.RvAdvancedBean
import kotlinx.android.synthetic.main.recylcer_view.*

/**
 *
 * @ClassName: AdvancedActivity
 * @Author: android 超级兵
 * @CreateDate: 10/22/21$ 10:01 AM$
 * TODO recyclerView进阶
 */
class RvAdvancedActivity : AppCompatActivity() {

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.recylcer_view)

        rootRecyclerView.layoutManager = LinearLayoutManager(this)

        val rvAdvancedParentAdapter = RvAdvancedParentAdapter(initData())

        rootRecyclerView.adapter = rvAdvancedParentAdapter

        val textView = TextView(this)
        textView.setBackgroundColor(Color.RED)
        textView.setTextColor(Color.WHITE)
        rvAdvancedParentAdapter.addFooterView(textView)

        rvAdvancedParentAdapter.parentCallBack = object : RvAdvancedParentAdapter.RvParentCallBack {
            override fun callBack() {
                val calculateTotalPrice = calculateTotalPrice(rvAdvancedParentAdapter)

                textView.text = "${calculateTotalPrice.first}\n\t总价为:${calculateTotalPrice.second}"
            }
        }


        val calculateTotalPrice = calculateTotalPrice(rvAdvancedParentAdapter)

        textView.text = "${calculateTotalPrice.first}\n\t总价为:${calculateTotalPrice.second}"

    }

    /*
     * 作者:android 超级兵
     * 创建时间: 10/22/21 12:18 PM
     * TODO 计算总价
     */
    fun calculateTotalPrice(rvAdvancedParentAdapter: RvAdvancedParentAdapter) = let {
        var tempStr = ""
        // 总价
        var totalPrice = 0
        rvAdvancedParentAdapter.data.flatMap {
            it.child
        }.map {
            it
        }.filter {
            it.isChildSelect
        }.map {
            totalPrice += it.price * it.number
            tempStr += "商品为:${it.title}价格为:${it.price}数量为:\t${it.number}\n"
        }

        Pair(tempStr, totalPrice)
    }

    private fun initData() = arrayListOf(
        RvAdvancedBean(false,
            "韩国三养旗舰店",
            listOf(
                RvAdvancedBean.ChildBean(6, 2, "火鸡面", true),
                RvAdvancedBean.ChildBean(3, 3, "方便面", false),
            )
        ),
        RvAdvancedBean(false,
            "nike京东自营店",
            listOf(
                RvAdvancedBean.ChildBean(799, 0, "跑鞋", false),
            )
        ),
        RvAdvancedBean(false,
            "李子柒官方店",
            listOf(
                RvAdvancedBean.ChildBean(12, 4, "螺蛳粉", true),
                RvAdvancedBean.ChildBean(18, 0, "蛋黄酱", false),
                RvAdvancedBean.ChildBean(20, 1, "牛肉酱", false),
            )
        ),
    )


}